#include<bits/stdc++.h>
using namespace std;
int n,a[200005],dp[2005][2005],ans;
signed main(){
	freopen("shung.in","r",stdin);
	freopen("shung.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=0;i<=n+1;i++)
		for(int j=0;j<=n+1;j++) dp[i][j]=-1;
	dp[0][n+1]=0;
	for(int i=1;i<=n;i++){
		for(int j=n+1;j>=0;j--){
			for(int k=0;k<=n+1;k++){
				if(dp[j][k]==-1) continue;
				if(j<a[i]) dp[a[i]][k]=max(dp[a[i]][k],dp[j][k]+1);
				if(a[i]<k) dp[j][a[i]]=max(dp[j][a[i]],dp[j][k]+1);
			}
		}
	}
	for(int i=0;i<=n+1;i++)
		for(int j=0;j<=n+1;j++) ans=max(dp[i][j],ans);
	cout<<ans;
	return 0;
}